package zsd.review.split;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import ICTCLAS.I3S.AC.ICTCLAS50;

import zsd.reiview.segment.option.SegmentOption;

/**
 * @Title: IctSpliting.java
 * @Package ICTCLAS.I3S.AC
 * @Description: TODO(添加描述)
 * @author 朱圣代
 * @date 2011-9-25 上午10:33:14
 * @version V1.0
 */

public class IctSpliting {

	ICTCLAS50 ICTCLAS50 = new ICTCLAS50();

	public IctSpliting() {

		Init();
	}

	public IctSpliting(boolean isInit) {
		if (isInit)
			Init();
	}

	String splited;
	List<String> wordsList;
	List<String> posList;
	List<Integer> chFrontBoundary;
	List<Integer> chBackBoundary;

	public boolean Init() {

		String argu = ".";
		// 初始化
		try {
			if (ICTCLAS50.ICTCLAS_Init(argu.getBytes(SegmentOption.UTF8)) == false) {
				System.out.println("Init Fail!");
				return false;
			}
			// 设置词性标注集(0 计算所二级标注集，1 计算所一级标注集，2 北大二级标注集，3 北大一级标注集)
			ICTCLAS50.ICTCLAS_SetPOSmap(SegmentOption.CASONELABLE);

			// 导入用户字典
			int nCount = 0;
			String usrdir = "userdict.txt"; // 用户字典路径
			byte[] usrdirb = usrdir.getBytes();// 将string转化为byte类型
			// 导入用户字典,返回导入用户词语个数第一个参数为用户字典路径，第二个参数为用户字典的编码类型
			nCount = ICTCLAS50.ICTCLAS_ImportUserDictFile(usrdirb, 0);
			// System.out.println("导入用户词个数" + nCount);
			// nCount = 0;
			// System.out.println("  分词工具初始化完成！......");
			return true;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}

	}

	public String splitToChars(String str) {
		wordsList = new ArrayList<String>();
		posList = new ArrayList<String>();
		chFrontBoundary = new ArrayList<Integer>();
		chBackBoundary = new ArrayList<Integer>();
		try {
			// 导入用户词典后分词
			byte nativeBytes[] = ICTCLAS50.ICTCLAS_ParagraphProcess(str.getBytes(SegmentOption.UTF8), 0, 1);// 分词处理
			// System.out.println(nativeBytes.length);
			String nativeStr = new String(nativeBytes, SegmentOption.UTF8);
			splited = nativeStr;
			String[] s = splited.split(" ");
			for (int i = 0; i < s.length; i++) {
				String[] ss = s[i].split("/");
				wordsList.add(ss[0]);
				posList.add(ss[1]);

				for (int j = 0; j < ss[0].length(); j++) {
					chFrontBoundary.add(0);
					chBackBoundary.add(0);
					if (j == 0)
						chFrontBoundary.add(1);
					if (j == ss[0].length() - 1)
						chBackBoundary.add(1);
				}
			}

			return nativeStr;

		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}

	public int[] tokenFirstPos;

	public List<String> splitToWords(String str) {
		wordsList = new ArrayList<String>();
		posList = new ArrayList<String>();
		chFrontBoundary = new ArrayList<Integer>();
		chBackBoundary = new ArrayList<Integer>();
		try {
			// 导入用户词典后分词
			byte nativeBytes[] = ICTCLAS50.ICTCLAS_ParagraphProcess(str.getBytes(SegmentOption.UTF8), 0, 1);// 分词处理
			// System.out.println(nativeBytes.length);
			String nativeStr = new String(nativeBytes, SegmentOption.UTF8);
			splited = nativeStr;
			String[] s = splited.split(" ");
			for (int i = 0; i < s.length; i++) {
				String[] ss = s[i].split("/");
				wordsList.add(ss[0]);
				if (ss.length < 2)
					posList.add("x");
				else
					posList.add(ss[1]);
			}
		//	posCorrect(wordsList, posList);
			int len = wordsList.size();
			tokenFirstPos = new int[len];
			int pos = 0;
			for (int i = 0; i < len; i++) {
				tokenFirstPos[i] = pos;
				pos += wordsList.get(i).length();
			}
			return wordsList;

		} catch (UnsupportedEncodingException e) {
			return wordsList;
		}
	}

	public int[] getTokenFirstPosList() {
		return tokenFirstPos;
	}

	public int getTokenFirstPos(int k) {
		return tokenFirstPos[k];
	}

	public List<String> getWordList() {
		return wordsList;
	}

	public int isFrontBound(int k) {
		return chFrontBoundary.get(k);
	}

	public int isbackBound(int k) {
		return chBackBoundary.get(k);
	}

	public List<String> getPosList() {
		return posList;
	}

	public String split(String str) {
		try {
			// 导入用户词典后分词
			byte nativeBytes[] = ICTCLAS50.ICTCLAS_ParagraphProcess(str.getBytes(SegmentOption.UTF8), 0, 1);// 分词处理
			// System.out.println(nativeBytes.length);
			String nativeStr = new String(nativeBytes, SegmentOption.UTF8);

			return nativeStr;

		} catch (UnsupportedEncodingException e) {
			return "";
		}
	}

	public static void posCorrect(List<String> toks, List<String> poss) {
		int len = toks.size();
		if (len < 2)
			return;
		for (int i = 1; i < len - 1; i++) {
			if (toks.get(i).equals("的")) {
				poss.set(i, poss.get(i - 1));
				poss.set(i + 1, "n");
				i++;
			} else if (toks.get(i).equals("地")) {
				poss.set(i, poss.get(i - 1));
				poss.set(i + 1, "a");
				i++;
			}
		}
		List<String> newToks = new ArrayList<String>();
		List<String> newPoss = new ArrayList<String>();
		newToks.add(toks.get(0));
		newPoss.add(poss.get(0));
		int k=1;
		for (int i = 1; i < len; i++) {
			String pre = poss.get(i -1 ).substring(0, 1);
			if (poss.get(i).startsWith( pre )) {
				String str = toks.get(i - 1) + toks.get(i);
				newToks.set(k - 1, str);
				newPoss.set(k - 1, pre );
			} else {
				newToks.add(toks.get(i));
				newPoss.add(poss.get(i));
				k++;
			}
		}
		toks = newToks;
		poss = newPoss;
	}
}
